/***** PROJECTED METHOD CODE *****/
/*****  Alexandre GAILLARD   *****/
/*   This version :: June 2017   */
/*********************************/


// AT THIS STAGE:: WE DON'T USE THE VALUE OF WW, Un, Ui, by assumption, FracskillE = 1.0 //


/****************************/
/** PROJECTED METHOD NEEDS **/
/****************************/

// COMMENT: becareful, loading should be made in the same order than the one in the file //

// LAST PERIOD VALUE FUNCTION // -- LOAD FILES
double *valueWWNET, *valueWWET, *valueUiNET, *valueUnNET, *valueUiET, *valueUnET;
double *valueJET, *valueJEiT, *valueJNET, *valueJNEiT;

valueJET    = (double *) calloc((ifulldimE), sizeof(double));
valueJNET   = (double *) calloc((ifulldimE), sizeof(double));
valueJEiT   = (double *) calloc((ifulldimE), sizeof(double));
valueJNEiT  = (double *) calloc((ifulldimE), sizeof(double));
valueWWNET  = (double *) calloc((ifulldimW), sizeof(double));
valueWWET   = (double *) calloc((ifulldimW), sizeof(double));
valueUnNET  = (double *) calloc((ifulldim), sizeof(double));
valueUnET   = (double *) calloc((ifulldim), sizeof(double));
valueUiNET  = (double *) calloc((ifulldim), sizeof(double));
valueUiET   = (double *) calloc((ifulldim), sizeof(double));

readinput(valueJET, ifulldimE, "OUTPUT/project_method/valueJE_T.out");
readinput(valueJNET, ifulldimE, "OUTPUT/project_method/valueJNE_T.out");
#if SEA == 1
    readinput(valueJEiT, ifulldimE, "OUTPUT/project_method/valueJEi_T.out");
    readinput(valueJNEiT, ifulldimE, "OUTPUT/project_method/valueJNEi_T.out");;
#endif
readinput(valueWWNET, ifulldimW, "OUTPUT/project_method/valueWWNE_T.out");
readinput(valueWWET, ifulldimW, "OUTPUT/project_method/valueWWE_T.out");
readinput(valueUnNET, ifulldim, "OUTPUT/project_method/valueUnNE_T.out");
readinput(valueUnET, ifulldim, "OUTPUT/project_method/valueUnE_T.out");
readinput(valueUiNET, ifulldim, "OUTPUT/project_method/valueUiNE_T.out");
readinput(valueUiET, ifulldim, "OUTPUT/project_method/valueUiE_T.out");


// LAST PERIOD DISTRIBUTIONS // -- LOAD FILES
double *distWWNE_T, *distWWE_T, *distUnE_T, *distUnNE_T, *distUiNE_T, *distUiE_T;
double *distJE_T, *distJEi_T, *distJNE_T, *distJNEi_T;

distJE_T    = (double *) calloc((ifulldimE), sizeof(double));
distJEi_T   = (double *) calloc((ifulldimE), sizeof(double));
distJNE_T   = (double *) calloc((ifulldimE), sizeof(double));
distJNEi_T  = (double *) calloc((ifulldimE), sizeof(double));
distWWNE_T  = (double *) calloc((ifulldimW), sizeof(double));
distWWE_T   = (double *) calloc((ifulldimW), sizeof(double));
distUiNE_T  = (double *) calloc((ifulldim), sizeof(double));
distUiE_T   = (double *) calloc((ifulldim), sizeof(double));
distUnNE_T  = (double *) calloc((ifulldim), sizeof(double));
distUnE_T   = (double *) calloc((ifulldim), sizeof(double));

readinput(distJE_T, ifulldimE, "OUTPUT/project_method/distJE_T.out");
readinput(distJNE_T, ifulldimE, "OUTPUT/project_method/distJNE_T.out");
#if SEA == 1
readinput(distJEi_T, ifulldimE, "OUTPUT/project_method/distJEi_T.out");
readinput(distJNEi_T, ifulldimE, "OUTPUT/project_method/distJNEi_T.out");
#endif
readinput(distWWNE_T, ifulldimW, "OUTPUT/project_method/distWWNE_T.out");
readinput(distWWE_T, ifulldimW, "OUTPUT/project_method/distWWE_T.out");
readinput(distUiE_T, ifulldim, "OUTPUT/project_method/distUiE_T.out");
readinput(distUiNE_T, ifulldim, "OUTPUT/project_method/distUiNE_T.out");
readinput(distUnE_T, ifulldim, "OUTPUT/project_method/distUnE_T.out");
readinput(distUnNE_T, ifulldim, "OUTPUT/project_method/distUnNE_T.out");


// FIRST PERIOD DISTRIBUTIONS // -- LOAD FILES
double *distWWNE_0, *distWWE_0, *distUnE_0, *distUnNE_0, *distUiNE_0, *distUiE_0;
double *distJE_0, *distJEi_0, *distJNE_0, *distJNEi_0;

distJE_0    = (double *) calloc((ifulldimE), sizeof(double));
distJEi_0   = (double *) calloc((ifulldimE), sizeof(double));
distJNE_0   = (double *) calloc((ifulldimE), sizeof(double));
distJNEi_0  = (double *) calloc((ifulldimE), sizeof(double));
distWWNE_0  = (double *) calloc((ifulldimW), sizeof(double));
distWWE_0   = (double *) calloc((ifulldimW), sizeof(double));
distUiNE_0  = (double *) calloc((ifulldim), sizeof(double));
distUiE_0   = (double *) calloc((ifulldim), sizeof(double));
distUnNE_0  = (double *) calloc((ifulldim), sizeof(double));
distUnE_0   = (double *) calloc((ifulldim), sizeof(double));

readinput(distJE_0, ifulldimE, "OUTPUT/project_method/distJE_0.out");
readinput(distJNE_0, ifulldimE, "OUTPUT/project_method/distJNE_0.out");
#if SEA == 1
readinput(distJEi_0, ifulldimE, "OUTPUT/project_method/distJEi_0.out");
readinput(distJNEi_0, ifulldimE, "OUTPUT/project_method/distJNEi_0.out");
#endif
readinput(distWWNE_0, ifulldimW, "OUTPUT/project_method/distWWNE_0.out");
readinput(distWWE_0, ifulldimW, "OUTPUT/project_method/distWWE_0.out");
readinput(distUiE_0, ifulldim, "OUTPUT/project_method/distUiE_0.out");
readinput(distUiNE_0, ifulldim, "OUTPUT/project_method/distUiNE_0.out");
readinput(distUnE_0, ifulldim, "OUTPUT/project_method/distUnE_0.out");
readinput(distUnNE_0, ifulldim, "OUTPUT/project_method/distUnNE_0.out");



/* DEFINE SAVING POLICY FUNCTION POINTER (TO FOLLOW POLICIES BETWEEN PERIODS == NEEDED TO COMPUTE SIMULATION) */
double *saveJNE, *searchJNE_W, *defaultJNE, *saveJE, *searchJE_W, *collateralJE, *sloanJNE, *collateralJNE;  // after knowning type zeta
double *saveJNEi, *searchJNEi_W, *defaultJNEi, *saveJEi, *searchJEi_W, *collateralJEi, *sloanJNEi, *collateralJNEi;  // after knowning type zeta
double *saveWWE, *searchWWE_J, *saveWWNE, *searchWWNE_J, *saveUiE, *searchUiE_J, *searchUiE_W, *saveUiNE, *searchUiNE_J, *searchUiNE_W, *saveUnE, *searchUnE_J, *searchUnE_W, *saveUnNE, *searchUnNE_J, *searchUnNE_W, *rateJNEi, *rateJNE;

saveJE          = (double *) calloc((ifulldimE*T), sizeof(double));
searchJE_W      = (double *) calloc((ifulldimE*T), sizeof(double));
saveJEi         = (double *) calloc((ifulldimE*T), sizeof(double));
searchJEi_W     = (double *) calloc((ifulldimE*T), sizeof(double));
saveJNE         = (double *) calloc((ifulldimE*T), sizeof(double));
searchJNE_W     = (double *) calloc((ifulldimE*T), sizeof(double));
saveJNEi        = (double *) calloc((ifulldimE*T), sizeof(double));
rateJNEi        = (double *) calloc((ifulldimE*T), sizeof(double));
rateJNE         = (double *) calloc((ifulldimE*T), sizeof(double));
searchJNEi_W    = (double *) calloc((ifulldimE*T), sizeof(double));
defaultJNE      = (double *) calloc((ifulldimE*T), sizeof(double));
defaultJNEi     = (double *) calloc((ifulldimE*T), sizeof(double));
sloanJNE        = (double *) calloc((ifulldimE*T), sizeof(double));
collateralJNE   = (double *) calloc((ifulldimE*T), sizeof(double));
collateralJE    = (double *) calloc((ifulldimE*T), sizeof(double));
sloanJNEi       = (double *) calloc((ifulldimE*T), sizeof(double));
collateralJNEi  = (double *) calloc((ifulldimE*T), sizeof(double));
collateralJEi   = (double *) calloc((ifulldimE*T), sizeof(double));

saveWWNE        = (double *) calloc((ifulldimW*T), sizeof(double));
searchWWNE_J    = (double *) calloc((ifulldimW*T), sizeof(double));
saveWWE         = (double *) calloc((ifulldimW*T), sizeof(double));
searchWWE_J     = (double *) calloc((ifulldimW*T), sizeof(double));

saveUiNE        = (double *) calloc((ifulldim*T), sizeof(double));
saveUnNE        = (double *) calloc((ifulldim*T), sizeof(double));
saveUiE         = (double *) calloc((ifulldim*T), sizeof(double));
saveUnE         = (double *) calloc((ifulldim*T), sizeof(double));
searchUiNE_J    = (double *) calloc((ifulldim*T), sizeof(double));
searchUnNE_J    = (double *) calloc((ifulldim*T), sizeof(double));
searchUnE_J     = (double *) calloc((ifulldim*T), sizeof(double));
searchUiE_J     = (double *) calloc((ifulldim*T), sizeof(double));
searchUiNE_W    = (double *) calloc((ifulldim*T), sizeof(double));
searchUnNE_W    = (double *) calloc((ifulldim*T), sizeof(double));
searchUnE_W     = (double *) calloc((ifulldim*T), sizeof(double));
searchUiE_W     = (double *) calloc((ifulldim*T), sizeof(double));



/* DEFINE TEMP POLICY FUNCTION FOR COMPUTING BACKWARD PRICE PATH */
double *temp_saveJNE, *temp_searchJNE_W, *temp_defaultJNE, *temp_saveJE, *temp_searchJE_W, *temp_collateralJE, *temp_sloanJNE, *temp_collateralJNE;  // after knowning type zeta
double *temp_saveJNEi, *temp_searchJNEi_W, *temp_defaultJNEi, *temp_saveJEi, *temp_searchJEi_W, *temp_collateralJEi, *temp_sloanJNEi, *temp_collateralJNEi;  // after knowning type zeta
double *temp_saveWWE, *temp_searchWWE_J, *temp_saveWWNE, *temp_searchWWNE_J, *temp_saveUiE, *temp_searchUiE_J, *temp_searchUiE_W, *temp_saveUiNE, *temp_searchUiNE_J, *temp_searchUiNE_W, *temp_saveUnE, *temp_searchUnE_J, *temp_searchUnE_W, *temp_saveUnNE, *temp_searchUnNE_J, *temp_searchUnNE_W, *temp_rateJNEi, *temp_rateJNE;

temp_saveJE         = (double *) calloc((ifulldimE*T), sizeof(double));
temp_searchJE_W     = (double *) calloc((ifulldimE*T), sizeof(double));
temp_saveJEi        = (double *) calloc((ifulldimE*T), sizeof(double));
temp_searchJEi_W    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_saveJNE        = (double *) calloc((ifulldimE*T), sizeof(double));
temp_searchJNE_W    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_saveJNEi       = (double *) calloc((ifulldimE*T), sizeof(double));
temp_searchJNEi_W   = (double *) calloc((ifulldimE*T), sizeof(double));
temp_defaultJNE     = (double *) calloc((ifulldimE*T), sizeof(double));
temp_defaultJNEi    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_sloanJNE       = (double *) calloc((ifulldimE*T), sizeof(double));
temp_collateralJNE  = (double *) calloc((ifulldimE*T), sizeof(double));
temp_collateralJE   = (double *) calloc((ifulldimE*T), sizeof(double));
temp_sloanJNEi      = (double *) calloc((ifulldimE*T), sizeof(double));
temp_collateralJNEi = (double *) calloc((ifulldimE*T), sizeof(double));
temp_collateralJEi  = (double *) calloc((ifulldimE*T), sizeof(double));
temp_rateJNEi       = (double *) calloc((ifulldimE*T), sizeof(double));
temp_rateJNE        = (double *) calloc((ifulldimE*T), sizeof(double));

temp_saveWWNE       = (double *) calloc((ifulldimW*T), sizeof(double));
temp_searchWWNE_J   = (double *) calloc((ifulldimW*T), sizeof(double));
temp_saveWWE        = (double *) calloc((ifulldimW*T), sizeof(double));
temp_searchWWE_J    = (double *) calloc((ifulldimW*T), sizeof(double));

temp_saveUiNE       = (double *) calloc((ifulldim*T), sizeof(double));
temp_saveUnNE       = (double *) calloc((ifulldim*T), sizeof(double));
temp_saveUiE        = (double *) calloc((ifulldim*T), sizeof(double));
temp_saveUnE        = (double *) calloc((ifulldim*T), sizeof(double));
temp_searchUiNE_J   = (double *) calloc((ifulldim*T), sizeof(double));
temp_searchUnNE_J   = (double *) calloc((ifulldim*T), sizeof(double));
temp_searchUnE_J    = (double *) calloc((ifulldim*T), sizeof(double));
temp_searchUiE_J    = (double *) calloc((ifulldim*T), sizeof(double));
temp_searchUiNE_W   = (double *) calloc((ifulldim*T), sizeof(double));
temp_searchUnNE_W   = (double *) calloc((ifulldim*T), sizeof(double));
temp_searchUnE_W    = (double *) calloc((ifulldim*T), sizeof(double));
temp_searchUiE_W    = (double *) calloc((ifulldim*T), sizeof(double));

/* DEFINE VALUE FUNCTION FOR SIMULATION */
double *valueJNE, *valueJNEi, *valueJE, *valueJEi;
double *valueWWE, *valueWWNE, *valueUiE, *valueUiNE, *valueUnE, *valueUnNE;

valueJE     = (double *) calloc((ifulldimE*T), sizeof(double));
valueJEi    = (double *) calloc((ifulldimE*T), sizeof(double));
valueJNE    = (double *) calloc((ifulldimE*T), sizeof(double));
valueJNEi   = (double *) calloc((ifulldimE*T), sizeof(double));
valueWWNE   = (double *) calloc((ifulldimW*T), sizeof(double));
valueWWE    = (double *) calloc((ifulldimW*T), sizeof(double));
valueUnNE   = (double *) calloc((ifulldim*T), sizeof(double));
valueUnE    = (double *) calloc((ifulldim*T), sizeof(double));
valueUiNE   = (double *) calloc((ifulldim*T), sizeof(double));
valueUiE    = (double *) calloc((ifulldim*T), sizeof(double));



/* DEFINE TEMP VALUE FUNCTION FOR COMPUTING BACKWARD PRICE PATH */
double *temp_valueJNE, *temp_valueJNEi, *temp_valueJE, *temp_valueJEi;
double *temp_valueWWE, *temp_valueWWNE, *temp_valueUiE, *temp_valueUiNE, *temp_valueUnE, *temp_valueUnNE;

temp_valueJE    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_valueJEi   = (double *) calloc((ifulldimE*T), sizeof(double));
temp_valueJNE   = (double *) calloc((ifulldimE*T), sizeof(double));
temp_valueJNEi  = (double *) calloc((ifulldimE*T), sizeof(double));
temp_valueWWNE  = (double *) calloc((ifulldimW*T), sizeof(double));
temp_valueWWE   = (double *) calloc((ifulldimW*T), sizeof(double));
temp_valueUnNE  = (double *) calloc((ifulldim*T), sizeof(double));
temp_valueUnE   = (double *) calloc((ifulldim*T), sizeof(double));
temp_valueUiNE  = (double *) calloc((ifulldim*T), sizeof(double));
temp_valueUiE   = (double *) calloc((ifulldim*T), sizeof(double));

double *temp_C_valueJNE, *temp_C_valueJNEi, *temp_C_valueJE, *temp_C_valueJEi;
double *temp_C_valueWWE, *temp_C_valueWWNE, *temp_C_valueUiE, *temp_C_valueUiNE, *temp_C_valueUnE, *temp_C_valueUnNE;

temp_C_valueJE    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_C_valueJEi   = (double *) calloc((ifulldimE*T), sizeof(double));
temp_C_valueJNE   = (double *) calloc((ifulldimE*T), sizeof(double));
temp_C_valueJNEi  = (double *) calloc((ifulldimE*T), sizeof(double));
temp_C_valueWWNE  = (double *) calloc((ifulldimW*T), sizeof(double));
temp_C_valueWWE   = (double *) calloc((ifulldimW*T), sizeof(double));
temp_C_valueUnNE  = (double *) calloc((ifulldim*T), sizeof(double));
temp_C_valueUnE   = (double *) calloc((ifulldim*T), sizeof(double));
temp_C_valueUiNE  = (double *) calloc((ifulldim*T), sizeof(double));
temp_C_valueUiE   = (double *) calloc((ifulldim*T), sizeof(double));


/* DEFINE DISTRIBUTION */
double *distJE, *distJNE, *distJEi, *distJNEi, *distWWNE, *distWWE, *distUnNE, *distUnE, *distUiE, *distUiNE;

distJE      = (double *) calloc((ifulldimE*T), sizeof(double));
distJEi     = (double *) calloc((ifulldimE*T), sizeof(double));
distJNE     = (double *) calloc((ifulldimE*T), sizeof(double));
distJNEi    = (double *) calloc((ifulldimE*T), sizeof(double));
distWWNE    = (double *) calloc((ifulldimW*T), sizeof(double));
distWWE     = (double *) calloc((ifulldimW*T), sizeof(double));
distUnNE    = (double *) calloc((ifulldim*T), sizeof(double));
distUnE     = (double *) calloc((ifulldim*T), sizeof(double));
distUiNE    = (double *) calloc((ifulldim*T), sizeof(double));
distUiE     = (double *) calloc((ifulldim*T), sizeof(double));



/* DEFINE TEMP DISTRIBUTION, FOR CURRENT AND NEXT TIME COMPUTATION */
double *temp_distJE, *temp_distJNE, *temp_distJEi, *temp_distJNEi, *temp_distWWNE, *temp_distWWE, *temp_distUnNE, *temp_distUnE, *temp_distUiE, *temp_distUiNE;

temp_distJE     = (double *) calloc((ifulldimE*T), sizeof(double));
temp_distJEi    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_distJNE    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_distJNEi   = (double *) calloc((ifulldimE*T), sizeof(double));
temp_distWWNE   = (double *) calloc((ifulldimW*T), sizeof(double));
temp_distWWE    = (double *) calloc((ifulldimW*T), sizeof(double));
temp_distUnNE   = (double *) calloc((ifulldim*T), sizeof(double));
temp_distUnE    = (double *) calloc((ifulldim*T), sizeof(double));
temp_distUiNE   = (double *) calloc((ifulldim*T), sizeof(double));
temp_distUiE    = (double *) calloc((ifulldim*T), sizeof(double));


double *temp_distJEnext, *temp_distJNEnext, *temp_distJEinext, *temp_distJNEinext, *temp_distWWNEnext, *temp_distWWEnext, *temp_distUnNEnext, *temp_distUnEnext, *temp_distUiEnext, *temp_distUiNEnext;

temp_distJEnext     = (double *) calloc((ifulldimE*T), sizeof(double));
temp_distJEinext    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_distJNEnext    = (double *) calloc((ifulldimE*T), sizeof(double));
temp_distJNEinext   = (double *) calloc((ifulldimE*T), sizeof(double));
temp_distWWNEnext   = (double *) calloc((ifulldimW), sizeof(double));
temp_distWWEnext    = (double *) calloc((ifulldimW), sizeof(double));
temp_distUnNEnext   = (double *) calloc((ifulldim), sizeof(double));
temp_distUnEnext    = (double *) calloc((ifulldim), sizeof(double));
temp_distUiNEnext   = (double *) calloc((ifulldim), sizeof(double));
temp_distUiEnext    = (double *) calloc((ifulldim), sizeof(double));
///////////////////////////////////

/******** END PROJECTED METHOD NEEDS ********/


